package cn.szzsi.common.kit;

import cn.szzsi.core.dubbo.WeChatDubboService;
import cn.szzsi.service.ApiInterface;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/11/9.
 */
public class WXShortUrl {
    /**
     *
     * @return
     */
    public static String generateShortUrl(String long_url,String shopId){
        String access_token = WeChatDubboService.getInstance().imports(ApiInterface.class).getAccessToken(shopId);
        String shortUrl="";
        try {
            String param = "{\"action\":\"long2short\","
                    + "\"long_url\":\""+long_url+"\"}";
            String sUrl="https://api.weixin.qq.com/cgi-bin/shorturl?access_token="+access_token;
            String result = HttpKit.post(sUrl, param);
            JSONObject jsonResult = new JSONObject(result);
            if(0==jsonResult.getInt("errcode")){
                shortUrl=jsonResult.getString("short_url");
            }else {
                System.out.println(jsonResult.getString("errmsg"));
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("生成渠道短链接异常");
        }
        if(StrKit.notBlank(shortUrl)){
           return  shortUrl;
        }else{
            return long_url;
        }
    }
}
